package capstoneproject.dudiachi.DataAccess;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

/**
 * 直接にデータベースを接続します。
 */

public class DBHelper extends SQLiteAssetHelper {
	// define some stuff in database
	public static final String DATABASE_NAME = "dudiachi.db";
	public static final int DATABASE_VERSION = 20;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		 setForcedUpgrade(DATABASE_VERSION);
	}

	private SQLiteDatabase database;

	/**
	 * <pre>
	 * コンストラクターを呼びます。
	 * データベースを作くります。｜接続を作ります。
	 * <pre>
	 */
	public DBHelper open() {
		database = getWritableDatabase();
		return this;
	}

	/**
	 * 開けたデータベースを閉めます。
	 */
	public void close() {
		database.close();
	}

	/**
	 * Get a cursor to the result of select query. It can be understand like
	 * this: SELECT * FROM tableName WHERE whereClause GROUP BY groupByClause
	 * ORDER BY orderByClause
	 * 
	 * @param tableName
	 * @param columns
	 *            A String Array that contain name of the column you want to
	 *            select.
	 * @param whereClause
	 *            include WHERE itself.
	 * @param orderByClause
	 *            include ORDER BY it self.
	 * @param groupByClause
	 *            include GROUP BY it self.
	 * @return
	 */
	public Cursor getData(String tableName, String[] columns,
			String whereClause, String orderByClause, String groupByClause)
			throws SQLiteException {
		return database.query(tableName, columns, whereClause, null,
				groupByClause, null, orderByClause);
	}

}
